// 2025/07/28
// tag: ST
// Author: Forever
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;
const int LOG = 21;
int n, m, f[MAXN][LOG], LOG2[MAXN];

char *p1, *p2, buf[100000];
#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++)
int read()
{
    int x = 0, f = 1;
    char ch = nc();
    while (ch < 48 || ch > 57) {
        if (ch == '-')
            f = -1;
        ch = nc();
    }
    while (ch >= 48 && ch <= 57)
        x = x * 10 + ch - 48, ch = nc();
    return x * f;
}

void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
    return;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    n = read(), m = read();
    for (int i = 1; i <= n; ++i)
        f[i][0] = read();
    LOG2[1] = 0, LOG2[2] = 1;
    for (int i = 3; i <= n; ++i)
        LOG2[i] = LOG2[i >> 1] + 1;
    for (int j = 1; j <= LOG; ++j)
        for (int i = 1; i + (1 << j) - 1 <= n; ++i)
            f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
    while (m--){
        int l, r;
        l = read(), r = read();
        int k = LOG2[r - l + 1];
        write(max(f[l][k], f[r - (1 << k) + 1][k]));
        putchar('\n');
    }
    return 0;
}